import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
// 导入路由组件
// import Login from '@/views/login'
// import Layout from '@/views/layout'
// import Home from '@/views/home'
// import Menu from '@/views/menu'
// import Role from '@/views/role'
// import Resource from '@/views/resource'
// import Course from '@/views/course'
// import User from '@/views/user'
// import Advert from '@/views/advert'
// import AdvertSpace from '@/views/advert-space'
// import ErrorPage from '@/views/error-page'
const original = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return original.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

// 需要验证的路由添加meta信息
const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login')
  },
  {
    path: '/',
    component: () => import('@/views/layout'),
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: '',
        name: 'home',
        redirect: '/role',
        component: () => import('@/views/home'),
        meta: {
          title: '首页'
        }
      },
      {
        path: '/menu',
        name: 'menu',
        component: () => import('@/views/menu'),
        meta: {
          title: '菜单列表'
        }
      },
      {
        path: '/role',
        name: 'role',
        component: () => import('@/views/role'),
        meta: {
          title: '角色列表'
        }
      },
      {
        path: '/resource',
        name: 'resource',
        component: () => import('@/views/resource'),
        meta: {
          title: '资源列表'
        }
      },
      {
        path: '/course',
        name: 'course',
        component: () => import('@/views/course'),
        meta: {
          title: '课程管理'
        }
      },
      {
        path: '/user',
        name: 'user',
        component: () => import('@/views/user'),
        meta: {
          title: '用户管理'
        }
      },
      {
        path: '/advert',
        name: 'advert',
        component: () => import('@/views/advert'),
        meta: {
          title: '广告'
        }
      },
      {
        path: '/advert-space',
        name: 'advert-space',
        component: () => import('@/views/advert-space'),
        meta: {
          title: '广告空间'
        }
      },
      {
        path: '/menu/create',
        name: 'menu-create',
        component: () => import('@/views/menu/create')
      },
      {
        path: '/menu/:id/edit',
        name: 'menu-edit',
        component: () => import('@/views/menu/edit')
      },
      {
        path: '/role/:roleId/alloc-menu',
        name: 'alloc-menu',
        component: () => import('@/views/role/alloc-menu'),
        props: true
      },
      {
        path: '/course/create',
        name: 'course-create',
        component: () => import('@/views/course/create')
      },
      {
        path: '/course/:courseId/edit',
        name: 'course-edit',
        component: () => import('@/views/course/edit')
      },
      {
        path: '/course/:courseId/section',
        name: 'course-section',
        component: () => import('@/views/course/section')
      }
    ]
  },
  {
    path: '*',
    name: 'error-page',
    component: () => import('@/views/error-page')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 跳转页面需要认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!store.state.user) {
      return next({
        name: 'login',
        query: {
          redirect: to.fullPath
        }
      })
    }
    next()
  } else {
    next()
  }
})

export default router
